Method: TZInfo::TimeOrDateTime#<=>

Defined in:
lib/tzinfo/time_or_datetime.rb

#<=>(timeOrDateTime) ⇒ Object

Compares this TimeOrDateTime with another Time, DateTime, timestamp (Integer) or TimeOrDateTime. Returns -1, 0 or +1 depending whether the receiver is less than, equal to, or greater than timeOrDateTime.

Returns nil if the passed in timeOrDateTime is not comparable with TimeOrDateTime instances.

Comparisons involving a DateTime will be performed using DateTime#<=>. Comparisons that don’t involve a DateTime, but include a Time will be performed with Time#<=>. Otherwise comparisons will be performed with Integer#<=>.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/tzinfo/time_or_datetime.rb', line 218

def <=>(timeOrDateTime)
  return nil unless timeOrDateTime.is_a?(TimeOrDateTime) || 
                    timeOrDateTime.is_a?(Time) ||
                    timeOrDateTime.is_a?(DateTime) ||
                    timeOrDateTime.respond_to?(:to_i)

  unless timeOrDateTime.is_a?(TimeOrDateTime)
    timeOrDateTime = TimeOrDateTime.wrap(timeOrDateTime)
  end
      
  orig = timeOrDateTime.to_orig
  
  if @orig.is_a?(DateTime) || orig.is_a?(DateTime)
    # If either is a DateTime, assume it is there for a reason 
    # (i.e. for its larger range of acceptable values on 32-bit systems).
    to_datetime <=> timeOrDateTime.to_datetime
  elsif @orig.is_a?(Time) || orig.is_a?(Time)
    to_time <=> timeOrDateTime.to_time
  else
    to_i <=> timeOrDateTime.to_i
  end
end